<?php

class Request extends Base
{
    private $controller, $action, $params, $route, $currentUrl, $get, $post, $session, $allParams=array(), $method;

    public function __construct($route, $currentUrl, $controllerName, $actionName, $requestParams)
    {
        $this->controller=$controllerName;
        $this->action=$actionName;
        $this->params=$requestParams;
        if($requestParams && $this->allParams)$this->allParams=array_merge($this->allParams,$requestParams);
        elseif($requestParams && !$this->allParams) $this->allParams=$requestParams;
        $this->route=$route;
        $this->currentUrl=$currentUrl;
        $this->session=new Session();
        if($_GET){
            foreach($_GET as $name=>$value){
                $this->get[$name]=$value;
                if(!isset($this->allParams[$name])) $this->allParams[$name]=$value;
                if(!isset(Sys::app()->controller->params[$name])) Sys::app()->controller->params[$name]=$value;
            }
        }
        if($_POST){
            foreach($_POST as $name=>$value){
                $this->post[$name]=$value;
                if(!isset($this->allParams[$name])) $this->allParams[$name]=$value;
            }
        }
        $this->method=$_SERVER['REQUEST_METHOD'];
    }

    public function createUrl($route,$params=array())
    {
        if(!$params && is_array($route)){
            $params=$route;
            $route=$this->route;
        }

        if($route=='index/index' || $route=='index/index/' || $route=='/index/index/' || $route=='/index/' || $route=='/index' || $route=='index'  || $route=='index/' || $route=='/') $route='';
        if($route==lcfirst($this->controller.'/index')) $route=lcfirst($this->controller);

        $url=BASEURL.$route;
        $lastNum=strlen($url)-1;
        if($url[$lastNum]=='/') $url=substr($url,0,$lastNum);

        foreach($params as $name=>$value){
            $url.='/'.$name.'/'.$value;
        }
        return $url;
    }

    public function getController()
    {
        return $this->controller;
    }
    public function getAction()
    {
        return $this->action;
    }
    public function getRoute()
    {
        return $this->route;
    }
    public function getCurrentUrl()
    {
        return $this->currentUrl;
    }
    public function getBaseUrl()
    {
        return BASEURL;
    }

    public function getParams()
    {
        return $this->params;
    }
    public function getGet()
    {
        return $this->get;
    }
    public function getPost()
    {
        return $this->post;
    }
    public function getSession()
    {
        return $this->session;
    }
    public function getAllParams()
    {
        return $this->allParams;
    }
    public function getMethod()
    {
        return $this->method;
    }

    public function param($name)
    {
        if(isset($this->params[$name])) return $this->params[$name];
        else return false;
    }
    public function get($name)
    {
        if(isset($this->get[$name])) return $this->get[$name];
        else return false;
    }
    public function post($name)
    {
        if(isset($this->post[$name])) return $this->post[$name];
        else return false;
    }
    public function session($name)
    {
        return $this->session->$name;
    }
    public function allParams($name)
    {
        if(isset($this->allParams[$name])) return $this->allParams[$name];
        else return false;
    }
    public function getIsAjax()
    {
        if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])=='xmlhttprequest') return true;
        else return false;
    }

    public static function redirect($route,$params=array())
    {
        if($route==404){
            Sys::app()->error='Страница не найдена. Адрес обращения: '.Sys::app()->request->route.', контроллер: '.Sys::app()->request->controller.', действите: '.Sys::app()->request->action;
            header('HTTP/1.1 404 Not Found');
            die('<div id="page-not-found">404<br />'.Sys::app()->error.'<br />Page not found</div>');
        }else{
            $url=BASEURL;

            if(is_array($route)){
                $params=$route;
                $url.=Sys::app()->controller->route;
            }elseif(strpos($route,'/')){
                $url.=$route;
            }elseif($route){
                $url.=strtolower(Sys::app()->request->controller).SEP.$route;
            }else{
                $url.=Sys::app()->controller->route;
            }

            if($params){
                foreach($params as $name=>$value){
                    $url.=SEP.$name.SEP.$value;
                }
            }

            header('Location: '.$url);
        }
    }
}